package com.yuanda.wordhot.util;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 当前线程 工具类
 * @author YuBai
 * @Description
 * @Date 2013-4-3
 * @version 1.0
 * @since 1.0
 */
public class ThreadLocalUtil {

	private static final ThreadLocal<Map<Serializable,Object>> context = new ThreadLocal<Map<Serializable,Object>>();

	private static Map<Serializable,Object> getLocalMap(String key, boolean isCreate) {
		Map<Serializable,Object> map = context.get();
		if ((map == null) && isCreate) {
			map = new HashMap<Serializable,Object>();
			context.set(map);
		}
		return map;
	}

	public static Object get(String key) {
		Map<Serializable,Object> map = getLocalMap(key, false);
		return map != null ? map.get(key) : null;
	}

	public static void set(String key, Object obj) {
		Map<Serializable,Object> map = getLocalMap(key, true);
		map.put(key, obj);
	}

	public static void remove(String key) {
		Map<Serializable,Object> map = getLocalMap(key, false);
		if (map != null) {
			map.remove(key);
		}
	}

	public static void removeAll() {
		Map<Serializable,Object> map = context.get();
		if (map != null) {
			map.clear();
		}
	}
}
